//
//  PictureBrowserCollectionViewCell.swift
//  SwiftWB
//
//  Created by 钟清 on 17/4/8.
//  Copyright © 2017年 Zhong Qing. All rights reserved.
//

import UIKit

class PictureBrowserCollectionViewCell: UICollectionViewCell,UIScrollViewDelegate {
    var imageURL: NSURL? {
        didSet {
            /* 下载提示开启 */
            indicatorView.startAnimating()
            /* 重置容器数据 */
            resetView()
            imageView.sd_setImageWithURL(imageURL) { (image, error, _, _) in
                /* 下载提示关闭 */
                self.indicatorView.stopAnimating()
                
                let width = UIScreen.mainScreen().bounds.width
                let height = UIScreen.mainScreen().bounds.height
                /* 图片等宽高比 */
                let scale = image.size.height / image.size.width
                /* 图片显示尺寸 */
                let imageHeight = width * scale
                self.imageView.frame = CGRect(origin: CGPointZero, size: CGSizeMake(width, imageHeight))
                /* 长短图显示位置 */
                if imageHeight < height {
                    /* 短图居中 */
                    let offsetY = (height - imageHeight) * 0.5
                    self.scrollView.contentInset = UIEdgeInsetsMake(offsetY, 0, offsetY, 0)
                } else {
                    /* 长图 */
                    self.scrollView.contentSize = CGSize(width: width, height: imageHeight)
                }
            }
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupCellUI()
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    // MARK: - 内部控制法方法
    private func setupCellUI() {
        /* 添加控件 */
        contentView.addSubview(scrollView)
        scrollView.addSubview(imageView)
        /* 布局 */
        scrollView.frame = UIScreen.mainScreen().bounds//self.frame
        scrollView.backgroundColor = UIColor(white: 0.3, alpha: 0.2)
    }
    /* 清空，防止item复用 */
    private func resetView() {
        scrollView.contentSize = CGSizeZero
        scrollView.contentInset = UIEdgeInsetsZero
        scrollView.contentOffset = CGPointZero
        imageView.transform = CGAffineTransformIdentity
    }
    // MARK: - Lazy
    private lazy var scrollView: UIScrollView = {
        let sc = UIScrollView()
        sc.maximumZoomScale = 2.0
        sc.minimumZoomScale = 0.5
        sc.delegate = self
        return sc
    }()
    /* 下载提示：用于图片浏览器的图片之间切换 */
    private lazy var indicatorView: UIActivityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.WhiteLarge)
    /* 图片视图 */
    lazy var imageView: UIImageView = UIImageView()
    
    // MARK: - UIScrollerViewDelegate
    /* 缩放的控件 */
    func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
        return imageView
    }
    /* 缩放的尺寸和位置 */
    func scrollViewDidZoom(scrollView: UIScrollView) {
        
        let width = UIScreen.mainScreen().bounds.width
        let height = UIScreen.mainScreen().bounds.height
        var offsetY = (height - imageView.frame.height) * 0.5
        var offsetX = (width - imageView.frame.width) * 0.5
        offsetY = (offsetY < 0) ? 0 : offsetY
        offsetX = (offsetX < 0) ? 0 : offsetX
        scrollView.contentInset = UIEdgeInsetsMake(offsetY, offsetX, offsetY, offsetX)
    }
}
